import re,redis
from django.shortcuts import render,redirect,HttpResponse
from django.views.decorators.csrf import csrf_exempt
from zjy_houqixiangmu import settings
from utils.send_mess import YunPian
from mainapp.models import Lunbotu
import datetime
# Create your views here.
def main(request):
    return render(request,"持明法洲管理系统.html")



# 获取验证码
@csrf_exempt
def get_code(request):
    """
    根据前台用户的手机号为用户生成随机验证码并完成发送
    :param request: 用户手机号
    :return:    发送成功与否
    """
    # 现获取手机号
    mobile = request.POST.get('mobile')

    # 获取到手机号后需要对手机号进行基本的正则验证
    if not re.match('^1[3456789]\d{9}$',mobile):
        return HttpResponse("请输入正确的手机号")

    # 先根据手机号去session比对手机号是否已经发送过短信以及时间戳是否满足再次发送的条件
    if request.session.get("mobile"):
        return HttpResponse("请勿在120s内重复发送")

    # 根据获取到手机号去发送短信
    yunpian = YunPian(settings.APIKEY)
    yunpian.send_message(mobile, "123456")

    # 向session中存入当前手机号与时间戳
    request.session['mobile'] = mobile
    return HttpResponse("发送成功")

@csrf_exempt
def add_banner(request):
    """
    添加轮播图的方法
    :param request:
    :return:
    """
    title = request.POST.get("title")
    status = request.POST.get("status")
    pic = request.FILES.get("pic")
    shengcheng_time = datetime.datetime.now().strftime('%Y-%m-%d')

    user = Lunbotu(jianjie=title, chatu=status, addr=pic, lbt_time=shengcheng_time)  # 创建对象
    user.save()

    return HttpResponse("1")